﻿using MediatR;
using WMS_Goodsinfo.Api.Read.Application.Command;
using WMS_Goodsinfo.Api.Read.DTOS;
using WMS_Goodsinfo.Domain;
using WMS_Goodsinfo.ErrorCode;
using WMS_Goodsinfo.Infastructure;

namespace WMS_Goodsinfo.Api.Read.Application.Handler
{
    public class SelectBatchflowHandler : IRequestHandler<SelectBatchflowCommand, ApiPaging<BatchFlowDto>>
    {
        private readonly IProductRepository<Product_Table> productRepo;
        private readonly IProductRepository<InOut> inoutRepo;

        public SelectBatchflowHandler(IProductRepository<Product_Table> productRepo, IProductRepository<InOut> inoutRepo)
        {
            this.productRepo = productRepo;
            this.inoutRepo = inoutRepo;
        }
        /// <summary>
        /// 查询批次流水
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<BatchFlowDto>> Handle(SelectBatchflowCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<BatchFlowDto> result = new ApiPaging<BatchFlowDto>();

            var product = productRepo.GetAll().Where(p => !p.IsDelete);
            var inout = inoutRepo.GetAll().Where(p => !p.IsDelete);

            if (!string.IsNullOrEmpty(request.Batch_Id))
            {
                inout = inout.Where(i => i.Batch_Id == request.Batch_Id);
            }
            // 应用过滤条件
            if (!string.IsNullOrEmpty(request.InOutNo))
            {
                inout = inout.Where(i => i.InOutNo.Contains(request.InOutNo));
            }

            if (!string.IsNullOrEmpty(request.InOutTiem))
            {
                inout = inout.Where(i => i.InOutTiem.Contains(request.InOutTiem));
            }

            if (!string.IsNullOrEmpty(request.BusinessType))
            {
                inout = inout.Where(i => i.BusinessType == request.BusinessType);
            }

            if (!string.IsNullOrEmpty(request.Inventory))
            {
                product = product.Where(p => p.Inventory.Contains(request.Inventory));
            }

            if (request.InNumber.HasValue)
            {
                inout = inout.Where(i => i.InNumber == request.InNumber.Value);
            }

            if (request.OutNumbber.HasValue)
            {
                inout = inout.Where(i => i.OutNumbber == request.OutNumbber.Value);
            }

            // 添加制单时间筛选
            if (request.CreateTime.HasValue)
            {
                inout = inout.Where(i => i.CreateTime.Value.Date == request.CreateTime.Value.Date);
            }

            var query = from i in inout
                        join p in product on i.Batch_Id equals p.Batch
                        select new BatchFlowDto
                        {
                            Id = i.Id,
                            ProductI_Id = p.Id,
                            InOutNo = i.InOutNo,
                            InOutTiem = i.InOutTiem,
                            Inventory = p.Inventory,
                            BusinessType = i.BusinessType,
                            InOutType = i.InOutType,
                            InNumber = i.InNumber,
                            OutNumbber = i.OutNumbber,
                            InMoney = i.InMoney,
                            OutMoney = i.OutMoney,
                            Batch_Id = i.Batch_Id,
                            Createer=i.Createer,
                            CreateTime = i.CreateTime,
                        };

            // 获取总数
            var totalCount = query.Count();

            // 分页查询
            var pageData = query
                .Skip((request.Pageindex - 1) * request.Pagesize)
                .Take(request.Pagesize)
                .ToList();

            result.Code = StatusEnum.OK;
            result.Message = "查询成功";
            result.TotalCount = totalCount;
            result.PageData = pageData;

            return Task.FromResult(result);
        }
    }
}
